<?php

    /**图片上传
     * @param $file 文件
     * @param $path 路径
     * @return mixed
     */
    function upload($file,$data){
        $info =  $file->move(ROOT_PATH . 'public/uploads' . DS . $data);
        if($info){
            return 'http://'.$_SERVER['SERVER_NAME'].'/uploads/'. $data.'/'.date('Ymd').'/'.$info->getFilename();
        }else{
            // 上传失败获取错误信息
            return $file->getError();
        }
    }


    //api 数据返回
    function return_msg($code, $msg = '', $data = []) {
        /*********** 组合数据  ***********/
        $return_data['code'] = $code;
        $return_data['msg']  = $msg;
        $return_data['data'] = $data;
        /*********** 返回信息并终止脚本  ***********/
        echo json_encode($return_data);die;
    }

    /**
     * 获取openid
     * @param   string  $appId     开发者id
     * @param   string  $appSecret 开发者密码
     * @param   string  $code 回调秘钥
     * @return  access_token
     */
    function getToken($appId,$appSecret,$code){
        $url = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$appId.'&secret='.$appSecret.'&js_code='.$code.'&grant_type=authorization_code';
        $jsonData = curl_request($url);
        $arrData = (array)json_decode($jsonData);
        return $arrData;
    }

    /**
     * curl请求接口
     * @param  string $url  请求地址
     * @param  string $data json数据
     * @return  返回值
     */
    function curl_request($url,$data=''){
        $curl = curl_init();
        curl_setopt($curl,CURLOPT_URL,$url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
        if(!empty($data)){
            // post数据
            curl_setopt($curl,CURLOPT_POST,1);
            //post的变量
            curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
        }
        $return = curl_exec($curl);
        curl_close($curl);
        return $return;
    }

    //生成TOKEN
    function create_token($phone)
    {
        $str = md5(uniqid(md5(microtime(true)), true) . $phone); //生成一个不会重复的字符串
        $str = sha1($str); //加密
        return $str;
    }

    //获取手机号
    function getwxphone($access_token,$code){
        $url = 'https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token='.$access_token;
        $data = [
            'code'              =>      $code
        ];
        $jsonData = curl_request($url,json_encode($data));
        $arrData = (array)json_decode($jsonData,true);
        if($arrData['errcode'] == 0){
            $phone = $arrData['phone_info']['phoneNumber'];
        }else{
            $phone = '';
        }
        return $phone;
    }

    //获取access_token
    function getaccesstoken(){
        $appId = config('AppId');
        $appSecret = config('appSecret');
        if(cache('access_token')){
            $access_token = cache('access_token');
        }else{
            $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appId.'&secret='.$appSecret;
            $json = curl_request($url);
            $return = (array)json_decode($json);
            $access_token = $return['access_token'];
            cache('access_token',$access_token,7000);
        }

        return $access_token;
    }

    //生成订单号
    function get_trade_no()
    {
        $str = rand(1000, 9999);
        return date("YmdHis") . $str;
    }

    //获取用户信息
    function getUserInfo($openid){
        $access_token = getaccesstoken();
        $url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN';
        $json = curl_request($url);
        $return = (array)json_decode($json);
        return $return;
    }

    /**
     * Notes:通过经纬度，计算出两个点的球面距离，返回单位:米
     * User: dong
     * DateTime: 2021/8/9 10:49
     * @param int $lat1 纬度1
     * @param int $lng1 经度1
     * @param int $lat2 纬度2
     * @param int $lng2 经度2
     * @return float
     */
    function host_getDistance($lat1=0, $lng1=0, $lat2=0, $lng2=0)
    {
        $EARTH_RADIUS = 6378137;   //地球半径
        $RAD = pi() / 180.0;
        $radLat1 = $lat1 * $RAD;
        $radLat2 = $lat2 * $RAD;
        $a = $radLat1 - $radLat2;    // 两点纬度差
        $b = ($lng1 - $lng2) * $RAD;  // 两点经度差
        $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
        $s = $s * $EARTH_RADIUS;
        $s = round($s * 10000) / 10000;
        return $s;
    }
